import { gameConfigHandle } from "core/config/gameConfig"
import Random from "core/utils/random"
import utils from "core/utils/utils"
import { PlayerModel } from "core/model/playerModel"
import { TalentNodeModel } from "core/model/talentModel"
import { GodNodeModel } from "core/model/godModel"
import { OccType } from "core/typeDefine/typeDefine"

class TalentHandle {
    create(player: PlayerModel, index: number, random: Random) {
        let talentConfig = gameConfigHandle.gameConfig.talent
        let y = talentConfig.list.length
        let list = []
        let listType3 = talentConfig.listType[3].slice(0)
        let nodeTypeLvlAmounts = [0, 0, 0, 0]
        for (let i = 0; i < talentConfig.nodeTypeLvl.length; i++) {
            let nodeTypeLvl = talentConfig.nodeTypeLvl[i]
            nodeTypeLvlAmounts[nodeTypeLvl] += 1
        }
        let attrisList = talentConfig.attrisList.slice(0)
        for (let i = 0; i < y; i++) {
            for (let j = 0; j < 6; j++) {
                if (player.talent.list[index] && player.talent.list[index][i] && player.talent.list[index][i]) {
                    let node = player.talent.list[index][i][j]
                    if (node.lock) {
                        let index = attrisList.findIndex(item => item == node.attriName)
                        attrisList.splice(index, 1)
                    }
                }
            }
        }
        attrisList = utils.getRandomArrayNoRepeat(talentConfig.attrisList, talentConfig.attrisList.length, random)
        for (let i = 0; i < y; i++) {
            let row = []
            for (let j = 0; j < 6; j++) {
                let nodeindex = i * 6 + j
                let nodeTypeLvl = talentConfig.nodeTypeLvl[nodeindex]
                let node
                if (player.talent.list[index] && player.talent.list[index][i] && player.talent.list[index][i]) {
                    node = player.talent.list[index][i][j]
                } else {
                    node = new TalentNodeModel()
                }
                node.type = nodeTypeLvl
                if ((nodeTypeLvl == 1 || nodeTypeLvl == 2) && !node.lock) {
                    node.attriName = attrisList.pop()
                }
                row.push(node)
            }
            list.push(row)

        }
        player.talent.list[index] = list
    }



    createGod(player: PlayerModel, index: number, random: Random) {
        let talentConfig = gameConfigHandle.gameConfig.talent
        let attriList = talentConfig.godAttriList.concat()
        let attriList2 = talentConfig.godAttriList2.concat()
        let occTypes = talentConfig.godOccTypes.concat()
        //单职业神格数量最多5个
        let godNodeAmountMaxOfOneOcc = talentConfig.godNodeAmountMaxOfOneOcc
        let togetOccTypes = []
        for (let i = 0; i < occTypes.length; i++) {
            for (let j = 0; j < godNodeAmountMaxOfOneOcc; j++) {
                togetOccTypes.push(occTypes[i])
            }
        }

        if (player.god.list[index]) {
            player.god.list[index].forEach(node => {
                if (node.lock) {
                    togetOccTypes.splice(togetOccTypes.indexOf(node.type), 1)
                }
            })
            player.god.list[index].forEach((node, i) => {
                if (!node.lock) {
                    let nodeTypeLvl = talentConfig.godNodeTypeLvl[i]
                    node.attriNames = []
                    node.attriNames.push(utils.getOneFromArray(attriList, random))
                    if (nodeTypeLvl == 1) {
                        node.attriNames.push(utils.getOneFromArray(attriList2, random))
                    }
                    node.type = utils.getOneFromArrayAndRemove(togetOccTypes, random)
                }
            })
            return
        }

        let list = []
        for (let i = 0; i < 25; i++) {
            let node = new GodNodeModel()
            let nodeTypeLvl = talentConfig.godNodeTypeLvl[i]
            node.attriNames.push(utils.getOneFromArray(attriList, random))
            if (nodeTypeLvl == 1) {
                node.attriNames.push(utils.getOneFromArray(attriList2, random))
            }
            node.type = utils.getOneFromArrayAndRemove(togetOccTypes, random)
            list.push(node)
        }
        player.god.list[index] = list
    }

    getGodTotalLvl(player: PlayerModel) {
        let totalLvl = 0
        Object.entries(player.god.nodeLvls).forEach(it => {
            let lvl = it[1] as number || 0
            totalLvl += lvl
        })
        return totalLvl
    }

    getGodLockNodeAmount(player: PlayerModel, index: number) {
        let lockNodeAmount = 0
        for (let i = 0; i < 25; i++) {
            if (player.god.list[index] && player.god.list[index][i] && player.god.list[index][i].lock) {
                lockNodeAmount++
            }
        }
        return lockNodeAmount
    }
    getGodPoint(player: PlayerModel, index: number) {
        let talentConfig = gameConfigHandle.gameConfig.talent
        let usePoint = 0
        let totalPoint = player.god.lvl * talentConfig.godLvlupPoint
        if (player.god.list[index]) {
            let list = player.god.list[index]
            for (let i = 0; i < list.length; i++) {
                usePoint += list[i].lvl
            }
        }
        return { usePoint, totalPoint, remainPoint: totalPoint - usePoint }
    }

    getTalentLockNodeAmount(player: PlayerModel, index: number) {
        let lockNodeAmount = 0
        let talentConfig = gameConfigHandle.gameConfig.talent
        let y = talentConfig.list.length
        for (let i = 0; i < y; i++) {
            for (let j = 0; j < 6; j++) {
                if (player.talent.list[index] && player.talent.list[index][i] && player.talent.list[index][i]) {
                    let node = player.talent.list[index][i][j]
                    if (node.lock) {
                        lockNodeAmount += 1
                    }
                }
            }
        }
        return lockNodeAmount
    }

    getTalentPoint(player: PlayerModel, index: number) {
        let talentConfig = gameConfigHandle.gameConfig.talent
        let rowPoints = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        let rowNeedPoints = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        let usePoint = 0
        let totalPoint = player.talent.lvl * talentConfig.lvlupPoint
        if (player.talent.list[index]) {
            let list = player.talent.list[index]
            for (let i = 0; i < list.length; i++) {
                let items = list[i]
                for (let j = 0; j < items.length; j++) {
                    let node = items[j]
                    if (node) {
                        let lvl = node.lvl || 0
                        let value = talentConfig.talentNodePoint[node.type] * lvl
                        rowPoints[i] += value
                        usePoint += value
                    }
                }
                if (i > 0) {
                    rowNeedPoints[i] = Math.ceil(list[i - 1].length * 5 / 2)
                }
            }
            return { usePoint, totalPoint, rowPoints, remainPoint: totalPoint - usePoint, rowNeedPoints }
        } else {
            return { usePoint, totalPoint, rowPoints, remainPoint: totalPoint - usePoint, rowNeedPoints }
        }

    }

    getNodeAroundPoint(player: PlayerModel, talentIndex: number, row: number, col: number) {
        let talentConfig = gameConfigHandle.gameConfig.talent
        let list = player.talent.list[talentIndex]
        let node = list[row][col]
        let aroundPoint = 0
        if (row > 4)
            if (list[row - 1][col]) {
                aroundPoint += list[row - 1][col].lvl || 0
            }

        if (row < 3) {
            if (list[row + 1][col]) {
                aroundPoint += list[row + 1][col].lvl || 0
            }
        }

        if (col > 3) {
            if (list[row][col - 1]) {
                aroundPoint += list[row][col - 1].lvl || 0
            }
        }
        if (col < 2) {
            if (list[row][col + 1]) {
                aroundPoint += list[row][col + 1].lvl || 0
            }
        }
        return aroundPoint

    }

    getNodeIsActive(player: PlayerModel, talentIndex: number, row: number, col: number) {
        let talentConfig = gameConfigHandle.gameConfig.talent
        let list = player.talent.list[talentIndex]
        let node = list[row][col]
        let nodeTypeLvl = talentConfig.nodeTypeLvl[row * 6 + col]
        if (nodeTypeLvl == 0) {
            return true
        }
        let needPoint = talentConfig.talentNodeNeedPrePoint[nodeTypeLvl]
        let nodeAroundPoint = talentHandle.getNodeAroundPoint(player, talentIndex, row, col)
        if (nodeAroundPoint >= needPoint) {
            return true
        } else {
            return false
        }
    }
}

let talentHandle = new TalentHandle()
export default talentHandle